package com.lei2j.state;

/**
 * Created by lei2j on 2019/2/4.
 */
public class GumballaMachine {

    private State soldoutState;

    private State noQuarterState;

    private State hasQuarterState;

    private State soldState;

    private State winnerState;

    State state = soldoutState;

    int count = 0;

    public GumballaMachine(int count) {
        this.count = count;
        soldoutState = new SoldOutState(this);
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);
        winnerState = new WinnerState(this);
        if (count>0){
            state = noQuarterState;
        }
    }


    public void insertQuarter(){
        state.insertQuarter();
    }

    public void ejectQuarter(){
        state.ejectQuarter();
    }

    public void turnCrank(){
        state.turnCrank();
        state.dispense();
    }

    public void setState(State state){
        this.state = state;
    }

    public State getSoldoutState() {
        return soldoutState;
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public State getSoldState() {
        return soldState;
    }

    public State getWinnerState() {
        return winnerState;
    }

    public void releaseBall(){
        System.out.println("发放糖果");
        if(count>0){
            count--;
        }
    }

    public int getCount(){
        return this.count;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("GumballaMachine{");
        sb.append("state=").append(state);
        sb.append(", count=").append(count);
        sb.append('}');
        return sb.toString();
    }
}
